/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.domain.das;

import cn.hallele.infrastructure.metadata.io.Query;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.usercenter.dto.TenantDTO;
import cn.hallele.mc.sdk.usercenter.dto.TenantQueryDTO;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.domain.domain.TenantDomain;
import cn.hallele.ms.portalcenter.integration.action.tenant.TenantIntegrationService;
import jakarta.validation.constraints.NotEmpty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * hallele_ms_usercenter
 *
 * @author anle5
 * @since 2024/10/13 14:34
 */
@Slf4j
@Service
public class TenantDDDas extends ModelDas<TenantIntegrationService, TenantDomain, TenantDTO> {
    protected TenantDDDas(TenantIntegrationService option) {
        super(option);
    }

    /**
     * 获取 租户列表
     *
     * @param condition 查询条件
     * @return List
     */
    public List<TenantDomain> findAll(TenantDomain condition) {
        return revertList(this.option.findAll(convert(condition)));
    }

    /**
     * 获取 租户列表
     *
     * @param condition 查询条件
     * @return List
     */
    public List<TenantDomain> findAllMoreCondition(TenantDomain condition) {
        return revertList(this.option.findAllMoreCondition(convertQueryDTO(condition)));
    }

    /**
     * 添加租户
     *
     * @param tenantDomain tenantDomain
     */
    public void addTenant(TenantDomain tenantDomain) {
        this.option.addTenant(convert(tenantDomain));
    }

    /**
     * 修改租户
     *
     * @param tenantDomain tenantDomain
     */
    public void updateTenant(TenantDomain tenantDomain) {
        this.option.updateTenant(convert(tenantDomain));
    }

    /**
     * 获取租户详情
     *
     * @param id id
     * @return TenantDomain
     */
    public TenantDomain getTenant(Integer id) {
        return revert(this.option.getTenant(id));
    }

    /**
     * 删除租户
     *
     * @param ids ids
     */
    public void deleteTenant(@NotEmpty List<Integer> ids) {
        this.option.deleteTenant(ids);
    }

    /**
     * TenantDomain -> TenantQueryDTO
     *
     * @param tenantDomain tenantDomain
     * @return TenantQueryDTO
     */
    private TenantQueryDTO convertQueryDTO(TenantDomain tenantDomain) {
        return ObjectUtil.map(tenantDomain, t -> TenantQueryDTO.builder()
                .status(t.getStatus())
                .pageNo(ObjectUtil.map(t.getQueryCondition(), Query::getPageNo))
                .pageSize(ObjectUtil.map(t.getQueryCondition(), Query::getPageNo))
                .keywords(ObjectUtil.map(t.getQueryCondition(), Query::getKeywords))
                .startTime(ObjectUtil.map(t.getQueryCondition(), Query::getStartTime))
                .endTime(ObjectUtil.map(t.getQueryCondition(), Query::getEndTime))
                .build());
    }

    /**
     * 将T转成E
     *
     * @param tenantDomain 被转换者
     * @return S 转换后者
     */
    @Override
    protected TenantDTO convert(TenantDomain tenantDomain) {
        return ObjectUtil.map(tenantDomain, t -> TenantDTO.builder()
                .id(t.getId())
                .name(t.getName())
                .parentId(t.getParentId())
                .treePath(t.getTreePath())
                .status(t.getStatus())
                .createTime(t.getCreateTime())
                .updateTime(t.getUpdateTime())
                .createBy(t.getCreateBy())
                .updateBy(t.getUpdateBy())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param tenantDTO 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected TenantDomain revert(TenantDTO tenantDTO) {
        return ObjectUtil.map(tenantDTO, t -> TenantDomain.builder()
                .id(t.getId())
                .name(t.getName())
                .parentId(t.getParentId())
                .treePath(t.getTreePath())
                .status(t.getStatus())
                .createTime(t.getCreateTime())
                .updateTime(t.getUpdateTime())
                .createBy(t.getCreateBy())
                .updateBy(t.getUpdateBy())
                .build());
    }
}